package com.okhttpdemo.http;

import android.app.ProgressDialog;
import android.content.Context;

import com.okhttpdemo.bean.MyResponse;
import com.okhttpdemo.utils.Logger;

import java.io.IOException;
import java.util.concurrent.Executor;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by 黄海 on 2017/3/1.
 */

public final class MyCall {
    private Call rawCall;
    private Executor executor;
    private ProgressDialog dialog;

    public MyCall(Call rawCall, Executor executor) {
        this.rawCall = rawCall;
        this.executor = executor;
    }

    public Request request() {
        return rawCall.request();
    }

    public Response execute() throws IOException {
        return rawCall.execute();
    }

    public void enqueue(Context context, final BaseCallBack<?> callBack) {
        this.enqueue(context, true, callBack);
    }

    public void enqueue(final Context context, final boolean progressDialogShow, final BaseCallBack<?> callBack) {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                callBack.onStart();
            }
        });
        if (progressDialogShow) dialog = HttpUtils.showProgressDialog(context);
        rawCall.enqueue(new Callback() {
            @Override
            public void onFailure(final Call call, final IOException e) {
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        callBack.onFailure(context, call, e);

                    }
                });
                onComplete();
            }

            @Override
            public void onResponse(final Call call, final Response response) throws IOException {
                Object body = null;
                try {
                    body = callBack.parseResponse(response);
                } catch (final ParseResponseException e) {
                    executor.execute(new Runnable() {
                        @Override
                        public void run() {
                            callBack.onFailure(context, call, e);
                        }
                    });
                    onComplete();
                    return;
                }
                final Object finalBody = body;
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            callBack.onResponse(call, new MyResponse(response, finalBody));
                        } catch (IOException e) {
                            Logger.e("onResponse 出现错误", call.request().url().toString());
                            e.printStackTrace();
                        }
                    }
                });
                onComplete();
            }

            void onComplete() {
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        if (progressDialogShow && dialog != null && dialog.isShowing())
                            dialog.dismiss();
                        callBack.onComplete();
                    }
                });
            }
        });
    }

    public void cancel() {
        rawCall.cancel();
    }

    public boolean isExecuted() {
        return rawCall.isExecuted();
    }

    public boolean isCanceled() {
        return rawCall.isCanceled();
    }
}
